package com.jt.provider.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalTime;

public class TimeInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        System.out.println("==preHandle==");
        //1.获取当前时间
        LocalTime now = LocalTime.now();//LocalTime为jdk8中的一个获取当前时间的api
        //2.获取当前的小时并进行逻辑判断
        int hour = now.getHour();//8:10~8
        System.out.println("hour="+hour);
        if(hour<6||hour>23){
            throw new RuntimeException("请在9~18时间范围内访问");//return false
        }
        return true;//false请求到此结束，true表示放行，会去执行后续的拦截器或controller对象
    }
}
